<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('employee_department_positions', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联关系
            $table->unsignedBigInteger('employee_id'); // 员工ID
            $table->unsignedBigInteger('department_id')->nullable(); // 部门ID
            $table->unsignedBigInteger('position_id')->nullable(); // 岗位ID

            // 名称快照（避免关联查询）
            $table->string('employee_name', 100)->nullable(); // 员工姓名
            $table->string('department_name', 100)->nullable(); // 部门名称
            $table->string('position_name', 100)->nullable(); // 岗位名称

            // 任职信息
            $table->date('start_date'); // 任职开始日期
            $table->date('end_date')->nullable(); // 任职结束日期
            $table->boolean('is_current')->default(true); // 是否当前任职

            // 任职类型
            $table->string('assignment_type', 20)->default('regular'); // 任职类型：regular, probation, temporary, transfer
            $table->string('reason', 200)->nullable(); // 任职原因
            $table->text('notes')->nullable(); // 备注

            // 薪资信息
            $table->decimal('base_salary', 10, 2)->nullable(); // 基本薪资
            $table->decimal('performance_bonus', 10, 2)->nullable(); // 绩效奖金
            $table->string('salary_grade', 20)->nullable(); // 薪资等级
            $table->string('salary_currency', 10)->default('CNY'); // 薪资货币

            // 工作信息
            $table->string('work_location', 100)->nullable(); // 工作地点
            $table->string('reporting_to', 100)->nullable(); // 汇报对象
            $table->json('working_hours')->nullable(); // 工作时间

            // 状态信息
            $table->string('status', 20)->default('active'); // 状态：active, inactive, terminated
            $table->date('termination_date')->nullable(); // 终止日期
            $table->text('termination_reason')->nullable(); // 终止原因

            // 审批信息
            $table->string('approved_by', 100)->nullable(); // 审批人
            $table->date('approved_at')->nullable(); // 审批时间
            $table->text('approval_notes')->nullable(); // 审批备注

            // 扩展字段
            $table->json('responsibilities')->nullable(); // 具体职责
            $table->json('kpis')->nullable(); // KPI指标
            $table->json('benefits')->nullable(); // 福利待遇

            // 排序
            $table->integer('sort_order')->default(0); // 排序

            // 索引
            $table->index(['employee_id']);
            $table->index(['department_id']);
            $table->index(['position_id']);
            $table->index(['start_date']);
            $table->index(['end_date']);
            $table->index(['is_current']);
            $table->index(['assignment_type']);
            $table->index(['status']);
            $table->index(['employee_id', 'is_current']); // 复合索引

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('employee_department_positions');
    }
};
